import TWEEN from '@tweenjs/tween.js'

/**
 * 补间动画
 */
export default class Tween {
  constructor(_viewer) {
    this.viewer = _viewer
  }

  /**
   * 移动相机视角
   * @param {object} target 相机目标位置
   * @param {number} duration 过渡时间
   */
  moveTo(target, duration = 2000) {
    const position = this.viewer.camera.position
    new TWEEN.Tween(position)
      .to(target, duration)
      .easing(TWEEN.Easing.Quadratic.Out)
      .onUpdate(() => {
        const { x, y, z } = position
        this.viewer.camera.position.set(x, y, z)
        // 保持 controls 控制器的焦点指向中心位置
        this.viewer.controls.target.set(0, 0, 0)
      })
      .start()
  }
}
